//package org.dromara.comm.Utils;
//import com.qiniu.common.QiniuException;
//import com.qiniu.http.Response;
//import com.qiniu.storage.BucketManager;
//import com.qiniu.storage.Configuration;
//import com.qiniu.storage.Region;
//import com.qiniu.storage.UploadManager;
//import com.qiniu.util.Auth;
//import org.apache.commons.lang.time.DateFormatUtils;
//import org.dromara.comm.exetion.BadRequestException;
//import org.dromara.comm.exetion.CommonConstants;
//import org.springframework.web.multipart.MultipartFile;
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.util.Date;
//import java.util.UUID;
//
///**
// *
// * 七牛云上传删除Utils
// *
// * @Author zhangzikang
// * @Date 2023/8/13 8:54
// */
//@org.springframework.context.annotation.Configuration
//public class QiniuUtil {
//
//    // 七牛云的accessKey
//    private static String accessKey = "bia0cM3uFDJ69kx_tzT255eeyNjQ2EomQuv5nKaT";
//
//    // 七牛云的secretKey
//    private static String secretKey = "_EhxA9ZRNm-JjR7Q6IdxdQeIOziG_v9A9xKTy3Fq";
//
//    // 七牛云的存储空间名称
//    private static String bucket = "commpro";
//
//    // 七牛云的存储空间链接
//    private static String path = "http://shfflvwvr.hb-bkt.clouddn.com";
//
//
//    /**
//     * 上传资料
//     * @param file 资料
//     * @return 返回资料路径
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     * @throws Exception
//     */
////    public String uploadInformation(MultipartFile file) throws Exception{
////        // 判断文件是否为空
////        if(file.isEmpty()) {
////            throw new BadRequestException(CommonConstants.FileIsEmpty);
////        }
////        //获取文件后缀
////        String originalFilename = file.getOriginalFilename();
////        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
////
////        //允许上传的文件格式集合
////        String[] suffixes = new String[]{".rar", ".zip"};
////        boolean bool = false;
////        //判断格式是否在suffixes中
////        for(String string : suffixes){
////            if (string.equalsIgnoreCase(suffix)){
////                bool = true;
////                break;
////            }
////        }
////        if(!bool){
////            throw new BadRequestException(CommonConstants.FormatError);
////        }
////        return getFile(suffix, file);
////    }
//
//    /**
//     * 上传视频
//     * @param file 视频
//     * @return 返回视频路径
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     * @throws Exception
//     */
////    public String uploadVideo(MultipartFile file) throws Exception{
////
////        // 判断文件是否为空
////        if(file.isEmpty()) {
////            throw new BadRequestException(CommonConstants.FileIsEmpty);
////        }
////        //获取文件后缀
////        String originalFilename = file.getOriginalFilename();
////        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
////
////        //判断格式是否正确
////        if(!suffix.equalsIgnoreCase(".mp4")){
////            throw new BadRequestException(CommonConstants.FormatError);
////        }
////        return getFile(suffix, file);
////    }
//
//    /**
//     * 上传简介
//     * @param file 简介
//     * @return 返回简介路径
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     * @throws Exception
//     */
////    public String uploadSynopsis(MultipartFile file) throws Exception{
////
////        // 判断文件是否为空
////        if(file.isEmpty()) {
////            throw new BadRequestException(CommonConstants.FileIsEmpty);
////        }
////
////        //获取文件后缀
////        String originalFilename = file.getOriginalFilename();
////        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
////
////        //允许上传的文件格式集合
////        String[] suffixes = new String[]{".doc", ".docx", ".docx"};
////        boolean bool = false;
////        //判断格式是否在suffixes中
////        for(String string : suffixes){
////            if (string.equalsIgnoreCase(suffix)){
////                bool = true;
////                break;
////            }
////        }
////        if(!bool){
////            throw new BadRequestException(CommonConstants.FormatError);
////        }
////        return getFile(suffix, file);
////    }
//
////    public String uploadImages(MultipartFile file,Long id) throws Exception{
////        // 判断图片是否为空或图片 大小是否大于限制
////        if(file.isEmpty()) {
////            throw new BadRequestException(CommonConstants.FileIsEmpty);
////        }else if(file.getSize() > CommonConstants.imgSize){
////            throw new BadRequestException(CommonConstants.imageSize);
////        }
////        //获取图片后缀
////        String originalFilename = file.getOriginalFilename();
////        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
////        //允许上传的图片格式集合
////        String[] suffixes = new String[]{".jpeg", ".jpg", ".png"};
////        boolean bool = false;
////        //判断格式是否在suffixes中
////        for(String string : suffixes){
////            if (string.equalsIgnoreCase(suffix)){
////                bool = true;
////                break;
////            }
////        }
////        if(!bool){
////            throw new BadRequestException(CommonConstants.FormatError);
////        }
////        // 上传图片并返回路径
////        return getFiles(suffix, file,id);
////    }
////    public String getFiles(String suffix,MultipartFile file,Long id) throws IOException {
////        //构造一个带指定 Region 对象的配置类
////        Configuration cfg = new Configuration(Region.autoRegion());
////        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;//指定分片上传版本
////        UploadManager uploadManager = new UploadManager(cfg);
////
////        //默认不指定key的情况下，以文件内容的hash值作为文件名
////        String key = getUploadFilesPath(suffix);
////        try {
////            byte[] uploadBytes = file.getBytes();
////            ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
////            Auth auth = Auth.create(accessKey, secretKey);
////            String upToken = auth.uploadToken(bucket);
////            try {
////                uploadManager.put(byteInputStream, key, upToken, null, null);
////            } catch (QiniuException ex) {
////                throw new BadRequestException(CommonConstants.KeyError);
////            }
////        } catch (UnsupportedEncodingException ex) {
////            throw new BadRequestException(CommonConstants.KeyError);
////        }
////        // 拼接路径地址
////        String fileUrl = String.format("%s/%s", path, key);
////        AtPicture atPicture = new AtPicture();
////        atPicture.setImagePath(fileUrl);
////        atPictureMapper.insert(atPicture);//添加上图片表数据
////
////        AtImagesFeedback atImagesFeedback = new AtImagesFeedback();
////        atImagesFeedback.setPictureId(atPicture.getId());
////        LambdaQueryWrapper<AtFeedback> queryWrapper = Wrappers.lambdaQuery();
////        queryWrapper.orderByDesc(AtFeedback::getCreateTime).last("limit 1");
////        AtFeedback atFeedback = atFeedbackMapper.selectOne(queryWrapper);
////        atImagesFeedback.setFeedbackId(atFeedback.getId());
////        atImagesFeedbackMapper.insert(atImagesFeedback);//添加图片与反馈中间表
////
////        return fileUrl;
////    }
//
//
//
//
//
//
//
//
//
//
//
//    /**
//     * 上传图片到七牛云
//     * @param file 图片
//     * @return 返回图片存储后的新图片名
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     * @throws Exception
//     */
//    public String uploadImage(MultipartFile file,Long id) throws Exception{
//
//        // 判断图片是否为空或图片大小是否大于限制
//        if(file.isEmpty()) {
//            throw new BadRequestException(CommonConstants.FileIsEmpty);
//        }else if(file.getSize() > CommonConstants.imgSize){
//            throw new BadRequestException(CommonConstants.imageSize);
//        }
//        //获取图片后缀
//        String originalFilename = file.getOriginalFilename();
//        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//        //允许上传的图片格式集合
//        String[] suffixes = new String[]{".jpeg", ".jpg", ".png"};
//        boolean bool = false;
//        //判断格式是否在suffixes中
//        for(String string : suffixes){
//            if (string.equalsIgnoreCase(suffix)){
//                bool = true;
//                break;
//            }
//        }
//        if(!bool){
//            throw new BadRequestException(CommonConstants.FormatError);
//        }
//        // 上传图片并返回路径
//        return getFile(suffix, file,id);
//    }
//
////    /**
////     * 上传图片到七牛云 并返回成封装数据
////     *
////     * @param file 图片
////     * @return 返回图片存储后的新图片名
////     * @throws Exception
////     * @Author zhangzikang
////     * @Date 2023/8/13 8:54
////     */
////    public ImageDto uploadImageDto(MultipartFile file, Long id) throws Exception {
////        //创建封装数据用于返回
////        ImageDto imageDto = new ImageDto();
////        imageDto.setErrno(0);
////
////        // 判断图片是否为空或图片大小是否大于限制
////        if (file.isEmpty()) {
////            throw new BadRequestException(CommonConstants.FileIsEmpty);
////        } else if (file.getSize() > CommonConstants.imgSize) {
////            throw new BadRequestException(CommonConstants.imageSize);
////        }
////        //获取图片后缀
////        String originalFilename = file.getOriginalFilename();
//////        String suffix = originalFilename.substring(originalF ilename.lastIndexOf("."));
////        int dotIndex = originalFilename.lastIndexOf(".");
////        if (dotIndex == -1) {
////            throw new BadRequestException(CommonConstants.FormatError);
////        }
////        String suffix = originalFilename.substring(dotIndex);
////
////        //允许上传的图片格式集合
////        String[] suffixes = new String[]{".jpeg", ".jpg", ".png"};
////        boolean bool = false;
////        //判断格式是否在suffixes中
////        for (String string : suffixes) {
////            if (string.equalsIgnoreCase(suffix)) {
////                bool = true;
////                break;
////            }
////        }
////        if (!bool) {
////            throw new BadRequestException(CommonConstants.FormatError);
////        }
////        //图片路径的封装类
////        ArrayList<UrlDto> urlDtos = new ArrayList<>();
////        //存放封装类
////        urlDtos.add(new UrlDto(getFile(suffix, file, id)));
////        imageDto.setData(urlDtos);
////
////        // 上传图片并返回路径
////        return imageDto;
////    }
//
//    /**
//     * 上传文件并返回路径//..
//     * @param suffix
//     * @param file
//     * @return
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     * @throws IOException
//     */
//    public String getFile(String suffix,MultipartFile file,Long id) throws IOException {
//        //构造一个带指定 Region 对象的配置类
//        Configuration cfg = new Configuration(Region.autoRegion());
////        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;//指定分片上传版本
//        UploadManager uploadManager = new UploadManager(cfg);
//
//        //默认不指定key的情况下，以文件内容的hash值作为文件名
//        String key = getUploadFilesPath(suffix);
//        try {
//            byte[] uploadBytes = file.getBytes();
//            ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
//            Auth auth = Auth.create(accessKey, secretKey);
//            String upToken = auth.uploadToken(bucket);
//            try {
//                uploadManager.put(byteInputStream, key, upToken, null, null);
//            } catch (QiniuException ex) {
//                throw new BadRequestException(CommonConstants.KeyError);
//            }
//        } catch (UnsupportedEncodingException ex) {
//            throw new BadRequestException(CommonConstants.KeyError);
//        }
//        // 拼接路径地址
//        String fileUrl = String.format("%s/%s", path, key);
////        AtStudentManage manage = new AtStudentManage();
////        manage.setId(id);
////        manage.setHeadSculpture(fileUrl);
////        atStudentManageMapper.updateById(manage);
//        return fileUrl;
//    }
//
//
//    /**
//     * 从七牛云删除文件
//     * @param fileName 删除文件
//     * @return 返回文件的删除结果
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     */
//    public void delFile(String fileName){
//        // 判断文件路径是否为空
//        if(fileName.isEmpty()){
//            throw new BadRequestException(CommonConstants.FileIsEmpty);
//        }
//
//        // 截取字符串长度
//        String substring = fileName.substring(path.length()+1);
//
//        //构造一个带指定 Region 对象的配置类
//        Configuration cfg = new Configuration(Region.region0());
//        Auth auth = Auth.create(accessKey, secretKey);
//        BucketManager bucketManager = new BucketManager(auth, cfg);
//        try {
//            //单次批量请求的文件数量不得超过1000
//            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
//            batchOperations.addDeleteOp(bucket, substring);
//            Response response = bucketManager.batch(batchOperations);
//            int status = response.statusCode;
//            // 判断上传状态是否成功
//            if (status != CommonConstants.status) {
//                throw new BadRequestException(CommonConstants.DeleteFailed);
//            }
//        } catch (QiniuException ex) {
//            throw new BadRequestException(CommonConstants.KeyError);
//        }
//    }
//
//
//
//    /**
//     * 生成新的文件名
//     * @return
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     */
//    private final String getUploadFilesPath(String suffix){
//        return datePath() + "/" + UUID.randomUUID().toString().replaceAll("-", "") + suffix;
//    }
//
//    /**
//     * 日期路径 即年/月/日 如2018/08/08
//     * @Author zhangzikang
//     * @Date 2023/8/13 8:54
//     */
//    private final String datePath() {
//        Date now = new Date();
//        return DateFormatUtils.format(now, "yyyy/MM/dd");
//    }
//}
